<template>
    <el-dialog @open="open" v-model="isOpen" :title="props.openType==='add'?'添加秒杀商品通知订阅':'修改秒杀商品通知订阅'" @close="close" style="width: 30%">
        <el-form ref="dataForm" :rules="rules" :model="form" label-width="100px" align="center">
            <el-form-item label="member_id" prop="memberId">
                <el-input :disabled ="props.openType==='edit'"  v-model="form.memberId"/>
            </el-form-item>
            <el-form-item label="sku_id" prop="skuId">
                <el-input :disabled ="props.openType==='edit'"  v-model="form.skuId"/>
            </el-form-item>
            <el-form-item label="活动场次id" prop="sessionId">
                <el-input :disabled ="props.openType==='edit'"  v-model="form.sessionId"/>
            </el-form-item>
            <el-form-item label="订阅时间" prop="subcribeTime">
                <el-input :disabled ="props.openType==='edit'"  v-model="form.subcribeTime"/>
            </el-form-item>
            <el-form-item label="发送时间" prop="sendTime">
                <el-input :disabled ="props.openType==='edit'"  v-model="form.sendTime"/>
            </el-form-item>
            <el-form-item label="通知方式[0-短信，1-邮件]" prop="noticeType">
                <el-input :disabled ="props.openType==='edit'"  v-model="form.noticeType"/>
            </el-form-item>
            <el-form-item>
                <el-button v-show='props.openType==="add"' type="primary" activity  @click="addSmsSeckillSkuNotice">添加</el-button>
                <el-button v-show="props.openType==='edit'" type="primary" activity  @click="updateSmsSeckillSkuNotice">更新</el-button>
                <el-button @click="close" type="success" activity >取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script setup>
    import {ref,reactive} from 'vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    const  emits = defineEmits(['close','reload'])

    let dataForm = ref(null)
    let props = defineProps(['openType','id'])
    let form = reactive({})
    let isOpen = ref(false)
    defineExpose({isOpen})

    const rules = reactive({
            memberId: [{required: true,message: '请输入member_id', trigger: 'blur'},],
            skuId: [{required: true,message: '请输入sku_id', trigger: 'blur'},],
            sessionId: [{required: true,message: '请输入活动场次id', trigger: 'blur'},],
            subcribeTime: [{required: true,message: '请输入订阅时间', trigger: 'blur'},],
            sendTime: [{required: true,message: '请输入发送时间', trigger: 'blur'},],
            noticeType: [{required: true,message: '请输入通知方式[0-短信，1-邮件]', trigger: 'blur'},],
    })
    function open(){
        if(props.openType==='edit'){
            request.get(`/api/smsSeckillSkuNotice/getSmsSeckillSkuNoticeById/${props.id}`).then(resp => {
                for(let key in resp){
                    form[key] = resp[key]
                }
            })
        }else if(props.openType==="add"){
            for(let key in form){
                delete form[key]
            }
        }
    }

    function close(){
        emits("close")
        dataForm.value.resetFields()//重置表单校验
    }

    function addSmsSeckillSkuNotice(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/smsSeckillSkuNotice/add",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '添加成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
    function updateSmsSeckillSkuNotice(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/smsSeckillSkuNotice/update",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '修改成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
</script>

<style scoped>

</style>